<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $subCategoryDetailArray = array();
	$categoryIdArr          = array();
	$categoryNameArr        = array();  
  $subCategoryName        = "";
  $i                      = 0;  
  $subCategoryId          = isset($_REQUEST['subCategoryId']) ? $_REQUEST['subCategoryId'] : 0;
  $categoryId             = isset($_REQUEST['categoryIdPopup']) ? $_REQUEST['categoryIdPopup'] : 0;
  $subCategorySelected    = (isset($_REQUEST['subCategoryName']) && $_REQUEST['subCategoryName'] != "") ? $_REQUEST['subCategoryName'] : "";
  $categorySelected       = (isset($_REQUEST['categoryIdPopup']) && $_REQUEST['categoryIdPopup'] != "") ? $_REQUEST['categoryIdPopup'] : "";

  // after post : start      
  if(isset($_POST['subCategoryName']))
  {
    if($subCategoryId > 0)
    {
      $updateSubCategory = "UPDATE subcategory 
                               SET categoryId       = ".$_POST['categoryIdPopup'].",
                                   subCategoryName  = '".$_POST['subCategoryName']."'
                             WHERE subCategoryId    = '".$_REQUEST['subCategoryId']."'";
      $updateSubCategoryRes  = mysql_Query($updateSubCategory);
      if(!$updateSubCategoryRes)
        die("Update Query Not Inserted : ".mysql_error());
//      else
//        header("Location:item.php?subCategorySelected=$subCategorySelected");
    }
    else
    {
      $insertSubCategory = "INSERT INTO subcategory (categoryId,subCategoryName)
                                  VALUE(".$_POST['categoryIdPopup'].",'".$_POST['subCategoryName']."')";
      $insertSubCategoryResult = mysql_query($insertSubCategory);
      if(!$insertSubCategoryResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
      {
        $subCategorySelectedAj = mysql_insert_id();
      }
    }
  }    
  // after post : end
  
  $selectSubCategory = "SELECT subcategory.subCategoryId,subcategory.subCategoryName,subcategory.categoryId,category.categoryId,category.categoryName
                          FROM subcategory
                          JOIN category
                         WHERE subcategory.categoryId = category.categoryId
                      ORDER BY category.categoryName,subcategory.subCategoryName";
  $selectSubCategoryResult = mysql_query($selectSubCategory);
  $subCategoryRow = mysql_num_rows($selectSubCategoryResult);
  while($subCategoryRow = mysql_fetch_array($selectSubCategoryResult))
  {
    $subCategoryDetailArray[$i]['subCategoryId']    = $subCategoryRow['subCategoryId'];
    $subCategoryDetailArray[$i]['categoryName']     = $subCategoryRow['categoryName'];
    $subCategoryDetailArray[$i]['subCategoryName']  = $subCategoryRow['subCategoryName'];
    
    if($subCategoryRow['subCategoryId'] == $subCategoryId)
    {
      $categoryName     = $subCategoryRow['categoryName'];  
      $subCategoryName  = $subCategoryRow['subCategoryName'];  
    }
    $i++;     
  }    

	////Select category : Start
	$selectCategoryQuery = "SELECT categoryId,categoryName
	                          FROM category
	                      ORDER BY categoryName";
	$selectCategoryQueryRes = mysql_query($selectCategoryQuery);
	$j = 0;
	while($categoryRow = mysql_fetch_array($selectCategoryQueryRes))
	{
	  if($j == 0 || (isset($_REQUEST['categoryIdPopup']) && $_REQUEST['categoryIdPopup'] == $categoryRow['categoryId']))
	    $categorySelected = $categoryRow['categoryId'];
	
	  $categoryIdArr[$j]     = $categoryRow['categoryId'];
	  $categoryNameArr[$j]   = $categoryRow['categoryName'];
	  $j++;
	}
	////Select category : End
	
  $selectQuery = "SELECT categoryId
	                  FROM subcategory
	                 WHERE subCategoryId =".$subCategoryId;
	$selectQueryRes = mysql_query($selectQuery);
	while($categoryCmbRow = mysql_fetch_array($selectQueryRes))
	{ 
	   $categorySelected  = $categoryCmbRow['categoryId'];  
   }
     
   
  include("./bottom.php");
  $smarty->assign("subCategoryId",$subCategoryId);
  $smarty->assign("subCategoryName",$subCategoryName);
  $smarty->assign("subCategoryDetailArray",$subCategoryDetailArray);
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("categoryIdArr",$categoryIdArr);
  $smarty->assign("categoryNameArr",$categoryNameArr);  
  $smarty->assign("i",$i);
  $smarty->display("subCategoryAdd.tpl");
}
?>